package org.streets.eis.component.grid;


import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.string.Strings;

import com.inmethod.grid.column.AbstractColumn;
import com.inmethod.grid.column.editable.EditableCellPanel;
import com.inmethod.grid.column.editable.EditablePropertyColumn;
import com.inmethod.grid.common.AbstractGrid;

public class EditableColumn extends EditablePropertyColumn {

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")	
	public EditableColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty) {
		super(columnId, headerModel, propertyExpression, sortProperty);
	}

	@SuppressWarnings("unchecked")	
	public EditableColumn(String columnId, IModel headerModel, String propertyExpression) {
		super(columnId, headerModel, propertyExpression);
	}

	@SuppressWarnings("unchecked")	
	public EditableColumn(IModel headerModel, String propertyExpression, String sortProperty) {
		super(headerModel, propertyExpression, sortProperty);
	}

	@SuppressWarnings("unchecked")	
	public EditableColumn(IModel headerModel, String propertyExpression) {
		super(headerModel, propertyExpression);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected EditableCellPanel newCellPanel(String componentId, IModel rowModel, IModel cellModel) {
		return new CellPanel(componentId, cellModel, rowModel, this);
	}
	
	@SuppressWarnings("unchecked")	
	@Override
	public boolean isLightWeight(IModel rowModel) {
		AbstractGrid grid = getGrid();
		boolean edit = (grid instanceof TableList) &&  ((TableList)grid).isEditAllRowsOnInitialization();
		if (edit) grid.setItemEdit(rowModel, edit);
		return super.isLightWeight(rowModel);
	}
	
	@SuppressWarnings("unchecked")
	protected FormComponent newEditableComponent(String id, final IModel model, IModel rowModel, AbstractColumn column){
		return new TextField(id,model);
	}
	
	public Component recreateCellPanel(WebMarkupContainer oldCellPanel){
		return newCell(null, oldCellPanel.getId(), oldCellPanel.getDefaultModel());
	}	
	
	public EditableComponentBehavior newEditableComponentBehavior(){
		return new EditableComponentBehavior();
	}
	
	@Override
	protected boolean isClickToEdit() {
		return false;
	}
		
	public static class EditableComponentBehavior extends AbstractBehavior{
		private static final long serialVersionUID = 1L;

		public void onComponentTag(final Component component, final ComponentTag tag){
			FormComponent<?> formComponent = (FormComponent<?>) component;
			if (formComponent.isValid() == false) {
				tag.put("class", "imxt-invalid");
				FeedbackMessage message = formComponent.getFeedbackMessage();
				if (message != null) {
					CharSequence title = tag.getString("title");
					if (Strings.isEmpty(title)){
						tag.put("title", message.getMessage().toString());						
					} else {
						tag.put("title", message.getMessage().toString() + "\r\n\r\n" + title);
					}
				}
			} else {
				if (formComponent.isRequired()){
					tag.put("class", "imxt-required");
				}
			}
		}		
	}
	
	public class CellPanel extends EditableCellPanel {
		private static final long serialVersionUID = 1L;

		@SuppressWarnings("unchecked")
		public CellPanel(String id, final IModel model, IModel rowModel, AbstractColumn column) {
			super(id, column, rowModel);
			setOutputMarkupId(true);
			FormComponent component = EditableColumn.this.newEditableComponent("editableField",model,rowModel, column);
			component.setOutputMarkupId(true);
			component.setLabel(column.getHeaderModel());
			component.add(EditableColumn.this.newEditableComponentBehavior());
			add(component);	
		}
		
		@SuppressWarnings("unchecked")		
		@Override
		protected FormComponent getEditComponent() {
			return (FormComponent) get("editableField");
		}
		
		@Override
		protected void onBeforeRender() {		
			super.onBeforeRender();
			
			AjaxRequestTarget target = AjaxRequestTarget.get();
			
			Component textField = get("editableField");
			
			if (target != null && isFocusTextField()) {
				target.focusComponent(textField);
			} 
		}
	}
	
}
